﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;

namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucAirConditonRoofCeiling.xaml
    /// </summary>
    public partial class ucAirConditonRoofCeiling : UserControl
    {
        public ucAirConditonRoofCeiling()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
         
        }

        public void loadData()
        {
            cbxCharacteristic.ItemsSource = LibraryManagerInCom.RoofCeilingCharacteristicList;
            cbxCharacteristic.DisplayMemberPath = "roof_ceiling_characteristic";
            cbxCharacteristic.SelectedIndex = 0;

            cbxStructure.ItemsSource = LibraryManagerInCom.RoofCeilingStructureList;
            cbxStructure.DisplayMemberPath = "roof_ceiling_structure";
            cbxStructure.SelectedIndex = 0;          
        }

        public void reload()
        {
            cbxCharacteristic.ItemsSource = null;
            cbxCharacteristic.Items.Clear();
            cbxStructure.ItemsSource = null;
            cbxStructure.Items.Clear();

            foreach (var item in LibraryManagerInCom.RoofCeilingCharacteristicList)
            {
                cbxCharacteristic.Items.Add(item);
            }
            foreach (var item in LibraryManagerInCom.RoofCeilingStructureList)
            {
                cbxStructure.Items.Add(item);
            }

            cbxCharacteristic.DisplayMemberPath = "roof_ceiling_characteristic";
            cbxCharacteristic.SelectedIndex = 0;

            cbxStructure.DisplayMemberPath = "roof_ceiling_structure";
            cbxStructure.SelectedIndex = 0;   
        }

        private void cbxCharacteristic_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadCoefficient();
        }

        private void cbxStructure_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadCoefficient();
        }
        public void loadCoefficient()
        {           
          
            if (cbxCharacteristic.SelectedIndex >=0 && cbxCharacteristic.SelectedIndex >=0)
            {
                int characteristicID = int.Parse(((tb_roof_ceiling_characteristic)cbxCharacteristic.SelectedItem).id.ToString());
                int structureID = int.Parse(((tb_roof_ceiling_characteristic)cbxCharacteristic.SelectedItem).id.ToString());
            
                tb_roof_ceiling_heat_coefficient co = LibraryManagerInCom.RoofCeilingHeatList.SingleOrDefault(a => a.structure_id == structureID && a.characteristic_id == characteristicID);
                if (co != null)
                {
                    textBox1.Text = co.heat_coefficient.ToString();
                }
                else
                {
                    textBox1.Text = "Not available...";
                }
                
            }
            reload();
            
        }

        public void newData()
        {
            cbxCharacteristic.IsEditable = true;
            cbxStructure.IsEnabled = true;
            textBox1.Text = "";

        }
        public void saveData()
        {
            tb_roof_ceiling_characteristic ch = new tb_roof_ceiling_characteristic();
            tb_roof_ceiling_structure st = new tb_roof_ceiling_structure();
            tb_roof_ceiling_heat_coefficient co = new tb_roof_ceiling_heat_coefficient();
            ch.roof_ceiling_characteristic = cbxCharacteristic.Text;
            st.roof_ceiling_structure = cbxStructure.Text;
            co.heat_coefficient = Convert.ToDouble(textBox1.Text);
            int chID = LibraryManagerInCom.addRoofCeilingCharacteristic(ch);
            int stID = LibraryManagerInCom.addRoofCeilingStructure(st);
            if (chID!=0 && stID !=0 )
            {
                co.characteristic_id = chID;
                co.structure_id = stID;
                LibraryManagerInCom.addRoofCeilingCoefficient(co);
            }
        }
        public void delData()
        {
        }
        public void copyData()
        {
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
      
    }
}
